package com.mxx.common.payment;

import com.mxx.common.model.EnumTemp;
import com.mxx.common.utils.RandomUtil;
import com.mxx.common.utils.date.DateUtil;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/8/14 16:10
 * @Description: 暂无描述
 */
public enum TradeType implements EnumTemp<TradeType> {
    Local("L", 0), Ali("ZFB", 1), WeChat("WX", 2), Apple("AP", 3);

    TradeType(String prefix, Integer payType) {
        this.prefix = prefix;
        this.payType = payType;
    }

    private Integer payType;
    private String prefix;

    public String generateTradeNo() {
        String tradeNo = generateTradeNo(20);
        return tradeNo;
    }

    public Integer getPayType() {
        return payType;
    }

    public String generateTradeNo(Integer length) {
        StringBuilder sb = new StringBuilder(this.prefix).append("-")
                .append(DateUtil.nowTimeMilliSecond());
        while (sb.length() < length) {
            sb.append(RandomUtil.randomOneNumber(9));
        }
        return sb.toString();
    }

    @Override
    public TradeType[] getValues() {
        return TradeType.values();
    }

    public boolean match(String tradeNo) {
        return tradeNo.startsWith(this.prefix);
    }

    public boolean match(Integer payType) {
        return payType == this.payType;
    }

    public static TradeType matchOutType(String tradeNo) {
        for (TradeType tradeType : TradeType.values()) {
            if (tradeType.match(tradeNo)) {
                return tradeType;
            }
        }
        return null;
    }

    public static TradeType matchOutType(Integer payType) {
        for (TradeType tradeType : TradeType.values()) {
            if (tradeType.match(payType)) {
                return tradeType;
            }
        }
        return null;
    }

}
